﻿namespace ES.Controllers
{
    using System;
    using System.Globalization;
    using System.Linq;
    using System.Web.Mvc;
    using ES.Models;
    using ePayment;

    public class ProductController : BaseController
    {
        //Ürün Listesi , KAtegoriye göre sıralama
        public ActionResult Index(string id, DefaultFilter filter)
        {
            ViewBag.Title = "Ürün Listesi";
            var list = Db.Products.AsQueryable();
            if (!string.IsNullOrEmpty(id))
            {
                list = list.Where(d => d.Category.SeoText.Equals(id));
                ViewBag.CategoryName = Db.Categories.Single(d => d.SeoText.Equals(id)).Text;
            }
            list = AddFilter(filter, list);
            return View(list.ToList());
        }

        public ActionResult Detail(string id)
        {
            var product = Db.Products.SingleOrDefault(d => d.Seo.Equals(id));
            if (product == null)
                throw new Exception("ürün bulunamadı");

            ViewBag.Title = product.Title;
            return View(product);
        }

        public ActionResult Sale(int id)
        {
            var product = Db.Products.SingleOrDefault(d => d.Id.Equals(id));
            if (product == null)
                return RedirectToAction("Index");
            return View(new UserCardInfo { Product = product });
        }
        [HttpPost]
        public ActionResult Sale(UserCardInfo cardInfo)
        {
            var product = Db.Products.SingleOrDefault(d => d.Id == cardInfo.Id);
            if (product == null)
                return RedirectToAction("Index");
            if (!ModelState.IsValid)
            {
                cardInfo.Product = product;
                return View(cardInfo);
            }
          
            ViewBag.Result = "";
            var payment = new cc5payment
                              {
                                  host = "host",
                                  name = "",
                                  password = "",
                                  clientid = "",
                                  orderresult = 0,
                                  cardnumber = cardInfo.CardNumber,
                                  expmonth = cardInfo.ExpMonth.ToString(),
                                  expyear = cardInfo.ExpYear.ToString(),
                                  cv2 = cardInfo.SecurityNumber,
                                  currency = "949",
                                  chargetype = "Auth",
                                  ip = Request.ServerVariables["REMOTE_ADDR"],
                                  subtotal = (((product.Price * product.Kdv) / 100) + product.Price).ToString(),
                                  taksit = "1"
                              };
            string bankaSonuc = payment.processorder();// Methot çağrılır
            string bankaHata = payment.errmsg;//dönen hata mesajı
            string bankaOid = payment.oid;//dönen order id
            string bankaAppr = payment.appr;// dönen işlem sonucu
            string bankaProv = payment.code;//dönen provizyon numarası

            if (bankaSonuc == "1")
            {
                if (bankaAppr == "Approved")//Ödeme işlemi tamam
                {
                    ViewBag.Result = "Banka ödemeyi kabl etti.Ödeme işlemi başarı ile tamamlandı.";
                }
                else if (bankaAppr == "Declined")//Ödeme işlemi reddedildi
                {
                    ViewBag.Result = "Banka ödemeyi reddetti.";
                }
                else
                {
                    ViewBag.Result = "Banka ile iletişim kurulamadı.";
                }
            }
            var sale = new Sale
            {
                CargoPrice = 20,
                CustomerId = CurrentCustomer.Id,
                ProductId = cardInfo.Id,
                RecordDate = DateTime.Now,
                SaleState = (short)SaleState.PendingCheck,
                BillAdress = cardInfo.BillAdress,
                DeliveryAdress = cardInfo.DeliveryAdress,
            };
            Db.Sales.InsertOnSubmit(sale);
            var basket = Db.CustomerOfBaskets.SingleOrDefault(d => d.CustomerId == CurrentCustomer.Id && d.ProductId == product.Id);
            if (basket != null)
                Db.CustomerOfBaskets.DeleteOnSubmit(basket);
            Db.SubmitChanges();
            return RedirectToAction("Index");
        }
    }
}
